<template>
  <div id="shoppingBag">
    <van-notice-bar mode="closeable" speed="50" color="#ff4001">温馨提示：当单件商品数量为1时，再点击减号将删除该商品！！！</van-notice-bar>
    <div class="content" v-show="show">
      <div class="contents">
        <img src="//newimgcdn.lecake.com/h5/images/shopping/empty.png" />
        <p>您还没有选购您的商品</p>
      </div>
    </div>
    <div class="cartList" v-for="(cartImg, cartId) in cartList" :key="cartId">
      <div class="isCheck">
        <Selector
          :isActive="cartImg.isChecked"
          @click="modifyIsChecked(cartImg.id)"
        ></Selector>
      </div>
      <div class="cartList_message">
        <router-link :to="'/detail/' + cartImg.id">
          <img :src="cartImg.shoppingBagImg" alt="" class="shoppingBagImg" />
        </router-link>
        <div class="cartList_detail">
          <h2>{{ cartImg.h1 }}</h2>
          <p class="fork">每份含餐具5套</p>
          <div class="money">
            <span>￥{{ cartImg.money }}</span>
          </div>
          <div class="input">
            <a
              href="javascript:void(0);"
              class="add"
              @click="upCartNum(cartImg.id, cartId)"
            ></a>
            <span class="num">{{ cartImg.num }}</span>
            <a
              href="javascript:void(0);"
              class="up"
              @click="addCartNum(cartImg.id)"
            ></a>
          </div>
        </div>
      </div>
      <div class="bagFooter" v-show="bagFooterShow">
        <div class="isChecked">
          <Selector
            :isActive="isCheckedAll"
            @click="modifyCheckedAll()"
          ></Selector>
        </div>
        <div class="btn_wrap">
          <a href="javascript:void(0);">结算</a>
        </div>
        <div class="total_price">
          <p class="price_wrap">
            应付：￥<strong class="total">{{ totalPrice }}</strong>
          </p>
        </div>
      </div>
    </div>
    <section class="margin_bottom">
      <div class="bag_title">
        <h2>推荐商品</h2>
        <h3>Sincerely Recommend</h3>
      </div>
      <ul class="list">
        <li
          class="common"
          v-for="(item, index) in shoppingList.data"
          :key="index"
        >
          <div class="item_info">
            <div class="message">
              <a href="#">
                <div class="img_warp">
                  <img :src="item.detailsImgUrl" alt="" />
                  <p class="corner_mark">
                    <span>{{ item.detailsTitle }}</span>
                  </p>
                </div>
              </a>
              <div class="item-detail">
                <div class="name-warp">
                  <h3>{{ item.detailsName }}</h3>
                  <h4>{{ item.discount }}</h4>
                </div>
                <div class="price_warp">
                  <p class="price">￥{{ item.detailsMoney }}</p>
                  <a href="javascript:void(0);"></a>
                </div>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </section>
  </div>
</template>

<script>
import axios from "axios";
import Selector from "../components/selector.vue";
export default {
  name: "ShoppingBag",
  data() {
    return {
      show: true,
      bagFooterShow: false,
      shoppingList: [],
      cartList: null,
    };
  },
  components: {
    Selector,
  },
  created() {
    axios
      .get(
        "https://www.fastmock.site/mock/942dfbb577a74c49a06b657043b1622a/lecake/lecakeShoppingBig"
      )
      .then((res) => {
        this.shoppingList = res.data;
      });
    if (localStorage.getItem("cartList")) {
      //从本地存储中获取购物车数据
      this.cartList = JSON.parse(localStorage.getItem("cartList")) || [];
    }
    if (JSON.parse(localStorage.getItem("cartList")).length == 0) {
      this.show = true;
      this.bagFooterShow = false;
    } else {
      this.show = false;
      this.bagFooterShow = true;
    }
  },
  methods: {
    // 减少单个商品的数量
    upCartNum(cartId, index) {
      // 遍历购物车数组
      this.cartList.forEach((goods) => {
        // 筛选指定商品
        if (cartId == goods.id) {
          if (goods.num > 1) {
            goods.num--;
            this.cartNum--;
            this.$store.state.cartNum--;
          } else if (goods.num == 1) {
            this.cartList.splice(index, 1);
            this.$store.state.cartNum--;
            this.$toast.success('删除成功');
          }
        }
      });
      
      localStorage.setItem("cartList", JSON.stringify(this.cartList));
      if (JSON.parse(localStorage.getItem("cartList")).length == 0) {
        this.show = true;
        this.bagFooterShow = false;
      }
    },
    // 增加单个商品的数量
    addCartNum(cartId) {
      // 遍历购物车数组
      this.cartList.forEach((goods) => {
        // 筛选指定商品
        if (cartId == goods.id) {
          goods.num++;
          this.cartNum++;
          this.$store.state.cartNum++;
        }
      });
      localStorage.setItem("cartList", JSON.stringify(this.cartList));
    },
    // 改变指定商品选中状态
    modifyIsChecked(goodsId) {
      // 遍历购物车数组
      this.cartList.forEach((goods) => {
        // 筛选指定商品
        if (goodsId == goods.id) {
          // 修改选中状态
          goods.isChecked = !goods.isChecked;
        }
      });
    },
    // 修改全部商品的全选状态
    modifyCheckedAll() {
      // 声明一个变量存储全选的反状态，该变量的值就是所有商品的isChecked的值
      let temp = !this.isCheckedAll;
      this.cartList.forEach((goods) => {
        goods.isChecked = temp;
      });
    },
  },
  computed: {
    // 全选的状态
    isCheckedAll: function () {
      return this.cartList.every((goods) => goods.isChecked);
    },
    // 计算总价
    totalPrice: function () {
      return this.cartList.reduce(
        (temp, goods) =>
          goods.isChecked ? temp + goods.money * goods.num : temp,
        0
      );
    },
  },
};
</script>

<style scoped="scoped">
#shoppingBag {
  width: 100%;
  position: relative;
  background: #ffffff;
  color: #3e3e3e;
  font-size: 0.12rem;
  margin: 0;
  padding: 0;
}
.content {
  position: relative;
  height: 4.4rem;
  text-align: center;
}
.content .contents img {
  position: absolute;
  width: 1.52rem;
  height: 1.52rem;
  left: 50%;
  top: 1.2rem;
  margin-left: -0.84rem;
}
.content .contents p {
  position: absolute;
  width: 100%;
  top: 2.8rem;
  font-size: 0.2rem;
  color: #9c9c9c;
}
.bag_title {
  border-top: 1px solid #d1c6af;
  padding: 0.4rem 0 0.32rem;
  color: #a38d6b;
  text-align: center;
  margin: 0 0.32rem;
}
.bag_title > h2 {
  text-align: center;
  height: 0.5rem;
  line-height: 0.5rem;
  font-weight: bold;
  font-size: 0.3rem;
}
.bag_title > h3 {
  font-weight: normal;
  font-size: inherit;
}
.list {
  list-style: none;
  margin: 0 0.32rem;
}
.common:nth-of-type(odd) {
  float: left;
  width: 48%;
}
.common:nth-of-type(even) {
  float: right;
  width: 48%;
}
.common {
  background: #ffffff;
  position: relative;
  font-size: 0.24rem;
  vertical-align: top;
}
.item_info {
  padding: 0 0 0.24rem;
}
.message {
  position: relative;
}
a,
a:hover {
  text-decoration: none;
}
.message > a {
  display: block;
  position: relative;
  width: 100%;
  padding-top: 100%;
}
.img_warp {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.img_warp img {
  width: 100%;
}
.corner_mark {
  position: absolute;
  width: 1.06rem;
  height: 1.08rem;
  left: -0.04rem;
  top: -0.24rem;
  background: url("../assets/cornerMark.png") no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
}
.corner_mark span {
  position: absolute;
  width: 100%;
  height: 0.24rem;
  line-height: 0.24rem;
  font-size: 10px;
  color: #483f35;
  transform: rotate(-45deg);
  left: -0.12rem;
  top: 0.26rem;
  overflow: hidden;
  text-align: center;
}
.item-detail {
  position: relative;
  height: 1.36rem;
  padding: 0.04rem 0;
}
.name-warp {
  height: 1rem;
  overflow: hidden;
  display: inline-block;
}
.name-warp h3 {
  font-size: 0.24rem;
  line-height: 0.4rem;
  overflow: hidden;
  white-space: initial;
  text-overflow: ellipsis;
  margin: 0;
  font-weight: normal;
}
.name-warp h4 {
  margin: 0;
  float: left;
  font-size: 0.2rem;
  color: #ffffff;
  border-radius: 2px;
  background-color: #cea461;
  position: relative;
  font-weight: normal;
}
.price_warp {
  position: relative;
  font-size: 0.28rem;
}
.price {
  margin: 0;
  color: #cea461;
  display: inline-block;
  margin-right: 0.04rem;
}
.price_warp > a {
  position: absolute;
  width: 0.6rem;
  height: 0.6rem;
  background: url("../assets/icons_5.png") no-repeat center;
  background-size: 6rem 6rem;
  top: 50%;
  margin-top: -0.3rem;
  background-position: -1.8rem -0.6rem;
  right: 0;
}
.bagFooter {
  position: fixed;
  z-index: 100;
  height: 1rem;
  left: 0;
  bottom: 1rem;
  border-bottom: 1px solid #d1c6af;
  width: 100%;
  background: #ffffff;
}
.isChecked {
  position: absolute;
  left: 0.4rem;
  top: 0.3rem;
  display: flex;
  align-items: center;
}
.isChecked::after {
  content: " 全选 ";
  position: absolute;
  width: 0.5rem;
  height: 0.16rem;
  line-height: 0.16rem;
  top: 50%;
  margin-top: -0.08rem;
  left: 0.55rem;
}
.total_price {
  float: right;
  text-align: right;
  margin-right: 0.16rem;
}
.price_wrap {
  height: 1rem;
  display: table-cell;
  vertical-align: middle;
}
.total {
  color: #ff4001 !important;
}
.btn_wrap {
  float: right;
}
.btn_wrap > a {
  display: inline-block;
  text-align: center;
  color: #fff;
  width: 1.8rem;
  height: 1rem;
  background-color: #ff4001;
  line-height: 1rem;
  font-weight: normal;
  font-size: .28rem;
}
.margin_bottom {
  display: inline-block;
  margin-bottom: 1rem;
}
/* 添加到购物车的商品样式 */
.cartList {
  padding: 0.2rem 0.4rem 0 1rem;
  position: relative;
  height: 2.2rem;
}
.isCheck {
  position: absolute;
  width: 0.8rem;
  height: 1rem;
  left: 0.2rem;
  top: 0.2rem;
  padding: 0.6rem 0 0;
}
.isCheck::after {
  content: "";
  clear: both;
}
.cartList_message {
  position: relative;
  float: left;
}
.shoppingBagImg {
  float: left;
  width: 1.8rem;
  height: 1.8rem;
}
.cartList_detail {
  position: relative;
  margin-left: 2rem;
  height: 1.8rem;
}
.cartList_detail > h2 {
  font-size: 0.24rem;
  font-weight: normal;
  margin: 0;
  height: 0.48rem;
  line-height: 0.48rem;
  padding: 0;
}
.cartList_detail > .fork {
  color: #9c9c9c;
  font-size: 0.1rem;
  padding-left: 0.22rem;
  position: relative;
}
.fork::after {
  content: "";
  position: absolute;
  width: 0.12rem;
  height: 0.18rem;
  background: url("../assets/icons2.png") no-repeat center;
  background-size: 4rem 4rem;
  top: 50%;
  margin-top: -0.1rem;
  background-position: -2.14rem -1.3rem;
  left: 0;
}
.money {
  position: absolute;
  left: 0;
  bottom: 0;
  display: inline-block;
  color: rgb(255, 64, 1);
}
.input {
  position: absolute;
  right: -132%;
  bottom: 0;
  width: 2.38rem;
  height: 0.5rem;
  border-radius: 2px;
  text-align: center;
  line-height: 0.5rem;
  display: flex;
}
.num {
  margin: 0 0.05rem;
}
.add,
.up,
.num {
  width: 0.76rem;
  height: 100%;
  background: #efefef;
  display: inline-block;
  position: relative;
}
.add::before {
  content: "";
  position: absolute;
  width: 0.2rem;
  height: 0.02rem;
  background: url("../assets/icons2.png") no-repeat center;
  background-size: 4rem 4rem;
  top: 70%;
  margin-top: -0.1rem;
  background-position: -0.1rem -0.18rem;
  left: 50%;
  margin-left: -0.1rem;
}
.up::before {
  content: "";
  position: absolute;
  width: 0.2rem;
  height: 0.2rem;
  background: url("../assets/icons2.png") no-repeat center;
  background-size: 4rem 4rem;
  top: 50%;
  margin-top: -0.1rem;
  background-position: -0.5rem -0.1rem;
  left: 50%;
  margin-left: -0.1rem;
}
</style>